package com.ruoyi.base.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.base.domain.SyType;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.base.domain.vo.SyTypeVo;
import com.ruoyi.base.domain.bo.SyTypeBo;
import com.ruoyi.base.service.ISyTypeService;

/**
 * 试验类型
 *
 * @author zyb
 * @date 2024-04-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/base/syType")
public class SyTypeController extends BaseController {

    private final ISyTypeService iSyTypeService;

    /**
     * 查询试验类型列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public R<List<SyTypeVo>> list(SyTypeBo bo) {
        List<SyTypeVo> list = iSyTypeService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 查询试验类型子集
     */
    @SaCheckLogin
    @GetMapping("/pageSub")
    public TableDataInfo<SyTypeVo> pageSub(SyTypeBo bo, PageQuery pageQuery) {
        return iSyTypeService.pageSub(bo, pageQuery);
    }

    /**
     * 导出试验类型列表
     */
    @SaCheckLogin
    @Log(title = "试验类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SyTypeBo bo, HttpServletResponse response) {
        List<SyTypeVo> list = iSyTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "试验类型", SyTypeVo.class, response);
    }

    /**
     * 获取试验类型详细信息
     *
     * @param syTypeId 主键
     */
    @SaCheckLogin
    @GetMapping("/{syTypeId}")
    public R<SyTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long syTypeId) {
        return R.ok(iSyTypeService.queryById(syTypeId));
    }

    /**
     * 新增试验类型
     */
    @SaCheckLogin
    @Log(title = "试验类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SyTypeBo bo) {
        return toAjax(iSyTypeService.insertByBo(bo));
    }

    /**
     * 修改试验类型
     */
    @SaCheckLogin
    @Log(title = "试验类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SyTypeBo bo) {
        return toAjax(iSyTypeService.updateByBo(bo));
    }

    /**
     * 删除试验类型
     *
     * @param syTypeIds 主键串
     */
    @SaCheckLogin
    @Log(title = "试验类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{syTypeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] syTypeIds) {
        return toAjax(iSyTypeService.deleteWithValidByIds(Arrays.asList(syTypeIds), true));
    }

    /**
     * 获取试验类型树 树列表
     */
    @SaCheckLogin
    @GetMapping("/syTpyeTreeBig")
    public R<List<Tree<Long>>> syTpyeTreeBig(SyType type) {
        return R.ok(iSyTypeService.syTpyeTreeBig(type));
    }

    /**
     * 获取试验类型树 树列表
     */
    @SaCheckLogin
    @GetMapping("/syTpyeTree")
    public R<List<Tree<Long>>> deptTree(SyType type) {
        return R.ok(iSyTypeService.selectTypeTreeList(type));
    }

    /**
     * 获取试验类型对应的明细列表
     * @param bo
     * @return
     */
    @SaCheckLogin
    @GetMapping("/listBySyTypeId")
    public R<Map> listBySyTypeId(SyTypeBo bo, PageQuery pageQuery) {
        return R.ok(iSyTypeService.listBySyTypeId(bo, pageQuery));
    }
}
